/**
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 */
package org.jiopi.ibean.kernel.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.junit.Test;
import org.jiopi.ibean.share.ShareUtil.IOUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class XMLMergerTestCase {
	
	@Test public void mergeXML_Right() throws Exception{
		InputStream conf_a = null;;
		InputStream conf_b = null;;
		try{
			conf_a = XMLMergerTestCase.class.getClassLoader().getResourceAsStream("org/jiopi/ibean/kernel/util/jiopi-config.xml");
			conf_b = XMLMergerTestCase.class.getClassLoader().getResourceAsStream("org/jiopi/ibean/kernel/util/jiopi-config-2.xml");
			assertNotNull(conf_a);
			assertNotNull(conf_b);
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			XMLMerger.mergeXMLToStream(conf_a, conf_b, out);
			String xmlString = new String(out.toByteArray());
			String correctString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<configuration xmlns=\"http://www.jiopi.org/2010/JIOPISchema\" xmlns:jiopi=\"http://www.jiopi.org/2010/JIOPISchema-configuration\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.jiopi.org/2010/JIOPISchema http://www.jiopi.org/2010/JIOPISchema/configuration/jiopi-config-configuration.xsd \">  \n  <module name=\"benz_car_sls_amg\" version=\"0.1\" replace-attribute=\"bb\" jiopi:replace-attribute=\"replace-attribute\"> \n    <controlpanel name=\"voyager.car\"> \n      <inner-accessory id=\"engine\" module=\"benz_engine_v9\" jiopi:replace-attribute=\"module\"/>  \n      <!-- install ETASocket -->  \n      <accessory id=\"etasocket\" controlpanel=\"voyager.etasocket\" module=\"common_etasocket_common\" version=\"0.1\"/>  \n      <socket from=\"etasocket\" to=\"engine\"/>  \n      <!-- install axletree  -->  \n      <accessory id=\"axletree\" controlpanel=\"voyager.axletree\" module=\"bena_axletree_amg\" version=\"0.1\"/>  \n      <socket from=\"axletree\" to=\"etasocket\"/>  \n      <!-- install wheels  -->  \n      <accessory id=\"wheel\" controlpanel=\"voyager.wheel\" module=\"benz_wheel_amg\" version=\"0.1\" retrieve=\"true\"/>  \n      <socket from=\"wheel\" to=\"axletree\" jiopi:replace-content=\"add\" jiopi:reset=\"true\">\n        <arg>\n          <value>0</value>\n        </arg>\n      </socket>\n      <socket from=\"wheel\" to=\"axletree\" jiopi:replace-content=\"add\">\n        <arg>\n          <value>1</value>\n        </arg>\n      </socket>\n    </controlpanel>  \n    <controlpanel name=\"voyager.car\" id=\"aa\" jiopi:template-except=\"id\"> \n      <inner-accessory id=\"engine\" module=\"benz_engine_v9\" jiopi:replace-attribute=\"module\"/>  \n      <!-- install ETASocket -->  \n      <accessory id=\"etasocket\" controlpanel=\"voyager.etasocket\" module=\"common_etasocket_common\" version=\"0.1\"/>  \n      <socket from=\"etasocket\" to=\"engine\"/>  \n      <!-- install axletree  -->  \n      <accessory id=\"axletree\" controlpanel=\"voyager.axletree\" module=\"bena_axletree_amg\" version=\"0.1\"/>  \n      <socket from=\"axletree\" to=\"etasocket\"/>  \n      <!-- install wheels  -->  \n      <accessory id=\"wheel\" controlpanel=\"voyager.wheel\" module=\"benz_wheel_amg\" version=\"0.1\" retrieve=\"true\"/>  \n      <socket from=\"wheel\" to=\"axletree\" jiopi:replace-content=\"add\" jiopi:reset=\"true\">\n        <arg>\n          <value>0</value>\n        </arg>\n      </socket>\n      <socket from=\"wheel\" to=\"axletree\" jiopi:replace-content=\"add\">\n        <arg>\n          <value>1</value>\n        </arg>\n      </socket>\n    </controlpanel>\n  </module> \n</configuration>\n";
			//xmlString = xmlString.replace("\"", "\\\"");
			//xmlString = xmlString.replaceAll("\n", "\\\\n");
			//System.out.println(xmlString);
			assertEquals(correctString,xmlString);
			
		}finally{
			IOUtil.close(conf_a);
			IOUtil.close(conf_b);
		}
	}
}
